package com.mzj.saas.rent;


import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.pojo.ApartmentCountInfo;
import com.haixiaoke.saas.repository.service.IApartmentService;
import com.mzj.saas.mservice.community.service.AppCommunityService;
import com.mzj.saas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 公寓 Controller
 * @author zhiping
 */
@RestController
@RequestMapping("/biz/apartment")
public class ApartmentController {


    @Autowired
    private IApartmentService apartmentService;

    @Autowired
    private AppCommunityService appCommunityService;

    @Autowired
    private LoginUserUtil loginUserUtil;

    @GetMapping("/detail/{houseId}")
    public AjaxResult detail(@PathVariable("houseId") String houseId) {
        return AjaxResult.success(apartmentService.getDetail(houseId));
    }

    @GetMapping("/sameCommunity/{houseId}")
    public AjaxResult sameCommunity(@PathVariable("houseId") String houseId) {
        return AjaxResult.success(apartmentService.getHousingInSameCommunityByHouseId(houseId));
    }

    @GetMapping("/countApartment")
    public AjaxResult countApartment(ApartmentCountInfo info) {

        String token = loginUserUtil.getToken();
        //权限过滤
        List<Map<String, Object>> communityList = appCommunityService.findCommunity(token).getResultData();
        if (communityList != null && !communityList.isEmpty()) {
            List<String> communityIds = communityList.stream().map(map -> map.get("communityId")
                    .toString()).collect(Collectors.toList());
            info.setCommunityIds(communityIds);
        }

        return AjaxResult.success(apartmentService.countApartment(info));
    }



}
